package org.zerg.controller.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.zerg.dto.AnotherFileDto;
import org.zerg.dto.FileDto;
import org.zerg.entity.Category;
import org.zerg.entity.Product;
import org.zerg.entity.Theme;
import org.zerg.service.CategoryService;
import org.zerg.service.ThemeService;

@Controller
@RequestMapping("/theme")
public class ThemeWebController {

	@Autowired
	private ThemeService themeService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private HttpSession session;
	
	@RequestMapping("/list")
	public String getThemeList(){
		List<Theme> list = themeService.getThemeList();
		session.setAttribute("list", list);
		return "/theme";
	}
	
	@RequestMapping("/modify")
	public String modifyTheme(FileDto dto,AnotherFileDto andto,@RequestParam("id")int id,@RequestParam("description")String description){
		if(dto==null||dto.getImgFile()==null||dto.getImgFile().getSize()<=0){
			themeService.modifyThemeNoTop(andto, id, description);
		}else if(andto==null||andto.getImgFile2()==null||andto.getImgFile2().getSize()<=0){
			themeService.modifyThemeNoHead(dto, id, description);
		}else if((dto.getImgFile()!=null&&dto.getImgFile().getSize()>0)&&(andto.getImgFile2()!=null||andto.getImgFile2().getSize()>0)){
			themeService.modifyTheme(dto, andto, id, description);
		}else{
			themeService.modifyThemeNoImage(id, description);
		}
		return "redirect:/theme/list";
	}
	
	@RequestMapping("/detaile")
	public String ThemeDetaile(@RequestParam("id")int id){
		List<Product> list = themeService.getProductByTheme(id);
		List<Category> list2 = categoryService.categoryAll();
		List<Theme> list3 = themeService.getThemeList();
		session.setAttribute("list", list);
		session.setAttribute("list2", list2);
		session.setAttribute("list3", list3);
		return "/theme_detaile";
	}
	
	@RequestMapping("/add")
	public String ThemeAddProduct(@RequestParam("themeid")int themeid,@RequestParam("classId")int classId){
		themeService.inserThemeOnProduct(themeid,classId);
		return "redirect:/theme/detaile?id="+themeid;
	}
	
	@RequestMapping("/delproduct")
	public String ThemeDeleteProduct(@RequestParam("id")int id,@RequestParam("desc")String desc){
		Theme theme = themeService.deleteProductOnTheme(id,desc);
		return "redirect:/theme/detaile?id="+theme.getId();
	}
	
}
